﻿@model CategoryNavigationModel.CategoryLineModel

@functions{

    public bool BreadCrumbContainsCurrentCategoryId(CategorySimpleModel category)
    {
        if (string.IsNullOrEmpty(Model.CurrentCategoryId))
            return false;

        if (category.Id == Model.CurrentCategoryId)
            return true;

        foreach (var subCategory in category.SubCategories)
        {
            if (BreadCrumbContainsCurrentCategoryId(subCategory))
            {
                return true;
            }
        }

        return false;
    }

}

@{
    var active = Model.Category.Id == Model.CurrentCategoryId || Model.Category.SubCategories.Count(BreadCrumbContainsCurrentCategoryId) > 0;
    var last = Model.Category.Id == Model.CurrentCategoryId;
    var liClass = active ? "active" : "inactive";
    if (last)
    {
        liClass += " last";
    }
}
<li class="@liClass">
    <a href="@Url.RouteUrl("Category", new { Model.Category.SeName })">
        @if (!string.IsNullOrEmpty(Model.Category.Icon))
        {
            <span class="category-icon @Model.Category.Icon"></span>
        }
        @Model.Category.Name
        @if (Model.Category.NumberOfProducts.HasValue)
        {
            <span class="total">@Loc["Categories.TotalProducts", Model.Category.NumberOfProducts.Value]</span>
        }
    </a>
    @{
        if (Model.Category.Id == Model.CurrentCategoryId ||
            Model.Category.SubCategories.Count(BreadCrumbContainsCurrentCategoryId) > 0)
        {
            if (Model.Category.SubCategories.Any())
            {
                <ul class="sublist">
                    @foreach (var subCategory in Model.Category.SubCategories)
                    {
                        var categoryLineModel = new CategoryNavigationModel.CategoryLineModel
                        {
                            CurrentCategoryId = Model.CurrentCategoryId,
                            Category = subCategory
                        };
                        <partial name="Components/Menu/Partials/CategoryLine.Navigation" model="categoryLineModel"/>
                    }
                </ul>
            }
        }
    }
</li>